package org.ezplatform.office.meeting.info.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ezplatform.cloud.api.system.org.OrgApiService;
import org.ezplatform.cloud.api.system.user.UserApiService;
import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.meeting.info.dao.MeetingScopeDao;
import org.ezplatform.office.meeting.info.entity.MeetingScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MeetingScopeService extends BaseService<MeetingScope, String>{
    
    @Autowired
    private MeetingScopeDao dao;
    
    @Autowired
    private UserApiService userApiService;
    
    @Autowired
    private OrgApiService orgApiService;
    
    @Override
    protected JpaBaseDao<MeetingScope, String> getEntityDao() {
        return dao;
    }
    
    /**
     * 查询table的scope字段关联的的用户、组织、部门、岗位、机构
     * @param fieldTableName
     * @param fieldData
     * @param fieldDataValues
     * @param b 
     * @return
     */
    public List<MeetingScope> getMeetingScopeList(String fieldTableName,String fieldData,List fieldDataValues, boolean flag){
        if(flag){
            return this.dao.getModelScopeList(fieldTableName,fieldData,fieldDataValues);
        }else{
            return null;
        }
    }

    public List<MeetingScope> getMeetingScopeList(String fieldDataValues){
    	return this.dao.getMeetingScopeList(fieldDataValues);
    }
    
    /**
     * 处理scopeList
     * @param emceeUserList
     * @param string
     * @param string2
     * @param emceeUser
     */
    public void saveCopeList(List<String> userList,List<String> orgList, String fieldDataTable, String fieldData, String fieldDataValue,String corpId,String token) {
        this.dao.deleteScope(fieldDataValue);
        List<MeetingScope> msList = new ArrayList<MeetingScope>();
        if(userList!=null && userList.size()>0){
            for(int i=0;i<userList.size();i++){
                String id = userList.get(i);
                Map<String, Object> map = this.userApiService.getUser(id, corpId, "", token);
                if(map != null){
                	MeetingScope ms = new MeetingScope();
                    ms.setScopetype("user");
                    ms.setType("ID");
                    ms.setScopevalue(String.valueOf(map.get("id")));
                    ms.setName(String.valueOf(map.get("userName")));
                    ms.setFielddatatable(fieldDataTable);
                    ms.setFielddata(fieldData);
                    ms.setFielddatavalue(fieldDataValue);
                    ms.setDeleteflag("0");
                    ms.setSortNo(i);
                    msList.add(ms);
                }
            }
        }
        if(orgList!=null && orgList.size()>0){
            for(int i=0;i<orgList.size();i++){
                String id = orgList.get(i);
                Map<String, Object> map = this.orgApiService.getOrg(id, corpId, "DD", token);
                if(map != null){
                	MeetingScope ms = new MeetingScope();
                    ms.setScopetype("org");
                    ms.setType("ID");
                    ms.setScopevalue(String.valueOf(map.get("id")));
                    ms.setName(String.valueOf(map.get("orgName")));
                    ms.setFielddatatable(fieldDataTable);
                    ms.setFielddata(fieldData);
                    ms.setFielddatavalue(fieldDataValue);
                    ms.setDeleteflag("0");
                    ms.setSortNo(i);
                    msList.add(ms);
                }
            }
        }
        if(msList!=null && msList.size()>0){
        	this.save(msList);
        }
    }
    
    public int updateScopeValue(String scopeValue, String receiveUserName, String id) {
    	return this.dao.updateScopeValue(scopeValue, receiveUserName, id);
    }
    
    public MeetingScope getModelScope(String scopeValue, String fielddatatable, String fieldData, String fieldDataValue) {
    	return this.dao.getModelScope(scopeValue, fielddatatable, fieldData, fieldDataValue);
    }
}